package yl.hs.bmipfull.service.host.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.production.IProductionLineDao;
import yl.hs.bmipfull.daos.host.IProductionLineSqlConfigDao;
import yl.hs.bmipfull.pojo.laboratory.TaskConfigDetailsEntity;
import yl.hs.bmipfull.pojo.laboratory.TaskConfigEntity;
import yl.hs.bmipfull.pojo.production.ProductionLineEntity;
import yl.hs.bmipfull.pojo.host.ProductionLineSqlConfigEntity;
import yl.hs.bmipfull.pojo.production.ProductionLinePortEntity;
import yl.hs.bmipfull.pojo.production.ProductionTaskEntity;
import yl.hs.bmipfull.pojo.production.TransTaskEntity;
import yl.hs.bmipfull.service.host.IProductionLineSqlConfigService;
import yl.hs.bmipfull.service.laboratory.ITaskConfigDetailsService;
import yl.hs.bmipfull.service.laboratory.ITaskConfigService;
import yl.hs.bmipfull.service.production.IProductionLinePortService;
import yl.hs.bmipfull.service.production.IProductionTaskService;
import yl.hs.bmipfull.service.production.ITransTaskService;
import yl.hs.bmipfull.service.system.impl.SystemBaseService;
import yl.hs.bmipfull.utils.CacheFactory;
import yl.hs.bmipfull.utils.ICache;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.utils.datastate.SqlMapFieldConfigType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作用:  生产线主机对接配置服务层接口实现<br/>
 * 创建时间: 2021年06月28日 14时54分41秒 <br/>
 *
 * @author 谢东
 */
@Service
public class ProductionLineSqlConfigServiceImpl extends SystemBaseService implements IProductionLineSqlConfigService {

    @Autowired
    IProductionLineSqlConfigDao dao;

    @Autowired
    IProductionLineDao lineDao;



    @Autowired
    IProductionTaskService taskService;

    @Autowired
    ITransTaskService transTaskService;

    @Autowired
    ITaskConfigService taskConfigService;

    @Autowired
    ITaskConfigDetailsService taskConfigDetailsService;

    @Autowired
    IProductionLinePortService linePortService;


    @Override
    public List<ProductionLineSqlConfigEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ProductionLineSqlConfigEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<ProductionLineSqlConfigEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(ProductionLineSqlConfigEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<ProductionLineSqlConfigEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public ProductionLineSqlConfigEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveProductionLineSqlConfig(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        ProductionLineSqlConfigEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new ProductionLineSqlConfigEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            //entity.setSn(queryNewSN(ProductionLineSqlConfigEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getPlSN(), "插入生产线主机对接配置信息", Tl.toJson(entity), ProductionLineSqlConfigEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新生产线主机对接配置信息", Tl.toJson(entity), ProductionLineSqlConfigEntity.class);
            }
        }

        return result;
    }

    @Override
    public Integer delete(String[] sn) {
        return null;
    }



    @Override
    public ProductionLineSqlConfigEntity findByLineName(String lineName) {
        return dao.findByLineName(lineName);
    }



}